package com.x.mvp.widget.cycleview;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * 自定义高度的viewpapger
 */
public class BaseViewPager extends ViewPager {
    private boolean scrollable = true;

    public BaseViewPager(Context context) {
        super(context);
    }

    public BaseViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 设置viewpager是否可以滚动
     *
     * @param enable
     */
    public void setScrollable(boolean enable) {
        scrollable = enable;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (scrollable) {
            return super.onInterceptTouchEvent(event);
        } else {
            return false;
        }
    }

    private float x, y;

    private boolean processTouchEvent(MotionEvent event) {
        boolean ret = false;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x = event.getX();
                y = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float dx = Math.abs(x - event.getX());
                float dy = Math.abs(y - event.getY());
                ret = dy > dx ? false : true;
                x = event.getX();
                y = event.getY();
                break;
        }
        return ret;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (processTouchEvent(ev)) {
            super.onTouchEvent(ev);
            return true;
        } else {

            return super.onTouchEvent(ev);
        }

    }
}